package com.qidianit.mes.md.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.common.pojo.MESResult;
import com.qidianit.common.util.Constant;
import com.qidianit.mes.md.mapper.WorkShopMapper;
import com.qidianit.mes.md.service.WorkShopService;
import com.qidianit.mes.pojo.WorkShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author 奇幻夜之梦
 * @Company 起点编程
 * @Description
 * @Time 2024/10/24 19:43
 */
@Service
public class WorkShopServiceImpl implements WorkShopService {
    @Autowired
    private WorkShopMapper workShopMapper;

    @Override
    public MESResult selectByPage(WorkShop workShop, Integer page, Integer limit) {
        //开启分页  参数1： 页数  参数2： 每页大小
        PageHelper.startPage(page,limit);
        //查询全部
        List<WorkShop> workShops = workShopMapper.selectByPage(workShop);
        //创建一个PageInfo对象，构造方法中传入查询的结果集合。
        PageInfo<WorkShop> pageInfo = new PageInfo<>(workShops);
        // 将pageInfo对象设置到QdResult中
        return MESResult.success(pageInfo);// 返回结果
    }

    @Override
    public MESResult save(WorkShop workShop) {
        if (workShop.getWorkShopCode() == null || workShop.getWorkShopCode() == "") {
            return MESResult.error("车库编码不能为空");
        }
        if (workShop.getWorkShopName() == null || workShop.getWorkShopName() == "") {
            return MESResult.error("车库名称不能为空");
        }
        workShop.setCreateTime(new Date());
        workShop.setModifyTime(new Date());
        return workShopMapper.save(workShop) == 1 ? MESResult.success(workShop):MESResult.error();
    }

    @Override
    public MESResult updateWorkShop(WorkShop workShop) {
        if (workShop.getWorkShopCode() == null || workShop.getWorkShopCode() == "") {
            return MESResult.error("车库编码不能为空");
        }
        if (workShop.getWorkShopName() == null || workShop.getWorkShopName() == "") {
            return MESResult.error("车库名称不能为空");
        }
        return workShopMapper.updateById(workShop) == 1?MESResult.success():MESResult.error();
    }

    @Override
    public MESResult selectById(Long workShopId) {
        return MESResult.success(workShopMapper.selectById(workShopId));
    }

    @Override
    public MESResult delete(Long workShopId) {
        // 修改状态
        return MESResult.isSuccess(workShopMapper.deleteById(workShopId));
    }

    @Override
    public MESResult deletes(String[] ids) {
        return MESResult.success(Constant.RESPONSE_STATUS_OK,"OK",workShopMapper.updateWorkShopsByIds(ids,Constant.DATA_STATUS_DELETE));
    }

    @Override
    public MESResult listWorkShops() {
        return MESResult.success(workShopMapper.listWorkShops());
    }
}

